import fs from 'node:fs'
import { Console } from 'node:console'
import path from 'node:path'
import config from '../../config.json' assert { type: 'json' }
import process from 'node:process'

const { logger: loggerConfig } = config
const filepath = path.join(process.cwd(), loggerConfig.outputFilePath)
const output = fs.createWriteStream(filepath)
const logger = new Console({
    stdout: output
})

function logWithDate(handler) {
    const _this = this
    return function(...args) {
        const prefix = `[${new Date().toLocaleString()} ${handler.name}]`
        handler.call(_this, prefix, ...args)
    }
}

logger.log = logWithDate(logger.log)
logger.info =logWithDate(logger.info)
logger.error = logWithDate(logger.error)
logger.warn = logWithDate(logger.warn)

export default logger
